from django.db import models

# Create your models here.

import group.models
from django.contrib.auth.models import User
from usermanage.models import UserInfo


class Blog(models.Model):
    permissionChoices = [
        (1, '任何人可见'),
        (2, '仅关注的人可见'),
        (3, '仅自己可见'),
    ]
    typeChoices = [
        (0, '草稿'),
        (1, '园内想法'),
        (2, '广场想法'),
        (3, '个人想法'),
    ]
    type = models.IntegerField(choices=typeChoices, default=0)
    userID = models.ForeignKey(User, on_delete=models.CASCADE)
    blogTitle = models.CharField(max_length=128)
    blogText = models.TextField(blank=True)
    commentNum = models.PositiveIntegerField(default=0)
    likeNum = models.PositiveIntegerField(default=0)
    readNum = models.PositiveIntegerField(default=0)
    creatTime = models.DateTimeField(auto_now_add=True)
    modifyTime = models.DateTimeField(auto_now=True)
    belongedGroup = models.ForeignKey(group.models.GroupInfo, on_delete=models.CASCADE, null=True, blank=True)
    visibility = models.BooleanField(default=True)
    permission = models.IntegerField(choices=permissionChoices, default=3)


class Comment(models.Model):
    commenter = models.ForeignKey(User, on_delete=models.CASCADE)
    commented = models.ForeignKey(Blog, on_delete=models.CASCADE)
    commentTime = models.DateTimeField(auto_now_add=True)
    visibility = models.BooleanField(default=True)
    commentText = models.TextField()


class Collect(models.Model):
    collector = models.ForeignKey(UserInfo, on_delete=models.CASCADE)
    collectBlog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    time = models.DateTimeField(auto_now=True)


class Like(models.Model):
    likeTime = models.DateTimeField(auto_now=True)
    likeUserID = models.ForeignKey(UserInfo, on_delete=models.CASCADE)
    beLikedBlog = models.ForeignKey(Blog, on_delete=models.CASCADE, null=True, blank=True)
    beLikedComment = models.ForeignKey(Comment, on_delete=models.CASCADE, null=True, blank=True)

    def __str__(self):
        return self.likeUserID.user.username + " likes " + self.beLikedBlog.blogTitle


class Drafts(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    draftTitle = models.CharField(max_length=256)
    draftText = models.TextField()
    saveTime = models.DateTimeField(auto_now=True)


class Report(models.Model):
    typeChoices = [
        (1, "举报用户"),
        (2, "举报评论"),
        (3, "举报文章"),
    ]
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    reportType = models.IntegerField(choices=typeChoices, null=True, blank=True)
    # 外键无法为空 用ID代替
    reportedCommentID = models.IntegerField(blank=True, null=True)
    reportedBlogID = models.IntegerField(blank=True, null=True)
    reportedUserID = models.IntegerField(blank=True, null=True)
    reason = models.TextField()
    reportTime = models.DateTimeField(auto_now_add=True)
    handled = models.BooleanField(default=False)
